package com.group3.group3.department.service.impl;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Tom
 * @Date: 2022/05/19/18:19
 * @Description:
 */

/**
 *@ClassName DepartmentServicelmpl
 *@Description
 *@Author 王琛
 *@Date 2022/5/19 18:19
 @Version 1.0
 */

import com.group3.group3.department.entity.SearchDepartmentEntity;
import com.group3.group3.department.repository.DepartmentDao;
import com.group3.group3.department.service.DepartmentService;
import com.group3.group3.mybatis.entity.PtDepartmentEntity;
import com.group3.group3.mybatis.flexigrid.PadingRstType;
import com.group3.group3.mybatis.flexigrid.PagingBean;
import com.group3.group3.mybatis.ztree.ZtreeEntity;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;
import java.util.UUID;


@Service
public class DepartmentServicelmpl implements DepartmentService {
    @Resource
    private DepartmentDao departmentDao;

    @Override
    public PadingRstType<PtDepartmentEntity> getDepartmentInfoListByPage(SearchDepartmentEntity search, PagingBean paging) {
        paging.deal(PtDepartmentEntity.class);
        PadingRstType<PtDepartmentEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());
        List<PtDepartmentEntity> list = departmentDao.queryDepartmentListByPage(search,paging);
        Integer total = departmentDao.queryDepartmentListTotal(search);
        padingRstType.setTotal(total);
        padingRstType.setRawRecords(list);
        padingRstType.putItems();

        return padingRstType;
    }

    @Override
    public List<ZtreeEntity> getMenuZtreeListByDepartmentId(String depUuid) {
        Locale locale = LocaleContextHolder.getLocale();
        List<ZtreeEntity> list = departmentDao.getMenuZtreeList(locale.toString());
        List<String> menuIdList = departmentDao.getMenuZtreeListByDepartmentId(depUuid);
        for(ZtreeEntity item:list){
            for(String menuId :menuIdList){
                if(menuId.equals(item.getId())){
                    item.setChecked(true);
                }
            }
        }

        return list;
    }

    @Override
    public void saveMenuByDepartmentId(String depUuid, String menuIds) {
        String[] menuArray = menuIds.split(",");
        departmentDao.deleteMenuByDepartment(depUuid);
        departmentDao.insertMenuRefDepartment(depUuid,menuArray);

    }

    @Override
    public void addDepartmentById(PtDepartmentEntity entity) {
        String uuid = UUID.randomUUID().toString().replace("-","");
        entity.setDepUuid(uuid);
        entity.setDelFlag("0");
        departmentDao.addDepartmentById(entity);



    }

}
